# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=csfml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=2.5.2
pkgrel=1
pkgdesc="A simple, fast, cross-platform, and object-oriented multimedia API for C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/SFML/CSFML"
license=('spdx:Zlib')
depends=("${MINGW_PACKAGE_PREFIX}-sfml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/SFML/CSFML/archive/${pkgver}.tar.gz
        mingw-w64-msys2.patch)
sha256sums=('2671f1cd2a4e54e86f7483c4683132466c01a6ca90fa010bc4964a8820c36f06'
            'e758cd07fcfc37b406f9625a79730043417d6809495c852af4c168d7a43287c5')

prepare() {
  cd ${srcdir}/CSFML-${pkgver}
  patch -p1 -i ${srcdir}/mingw-w64-msys2.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCSFML_LINK_SFML_STATICALLY=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DCSFML_BUILD_DOC=ON \
    ../CSFML-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package_csfml() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "${srcdir}/build-${MSYSTEM}"
  install -Dm644 "${srcdir}/CSFML-${pkgver}/license.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/CSFML dest${MINGW_PREFIX}/share/CSFML
}

package_csfml-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
